package com.py.warehouse.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.py.api.PyCodeApi;
import com.py.warehouse.domain.*;
import com.py.warehouse.service.*;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 盘点列表Controller
 * 
 * @author 姜威
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/check")
public class WhInventoryCheckController extends BaseController
{
    @Autowired
    private IWhInventoryCheckService whInventoryCheckService;

    @Autowired
    private WhInventoryService whInventoryService;

    @Autowired
    private ProductService productService;

    @Resource
    private PyCodeApi pyCodeApi;

    @Resource
    private IPyAttachmentService pyAttachmentService;

    @Resource
    private IWhCheckDetailsService whCheckDetailsService;

    /**
     * 获取编码
     */
    @PostMapping("/getcode")
    public Map<String, Object> getCode(@RequestParam(value = "code") Integer code){
        System.out.println("进来了"+code);
        return pyCodeApi.generateCode(code);
    }



    /**
     * 查找库位里面的商品数量
     */
    @RequiresPermissions("warehouse:check:listProduct")
    @GetMapping("/listProduct")
    public TableDataInfo list(@RequestParam("warehouseCode") String warehouseCode,
                              @RequestParam(value = "productName", required = false) String productName,
                              @RequestParam(value = "productUnit", required = false) Integer productUnit)
    {
        startPage();
        System.out.println(warehouseCode);
        System.out.println(productName);
        System.out.println(productUnit);
        List<Product> list =  whInventoryService.selectProductByWarehouseCode(warehouseCode, productName, productUnit);
        System.out.println("list:" + list);
        return getDataTable(list);
    }

    /**
     * 查询盘点列表列表
     */
    @RequiresPermissions("warehouse:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo list(WhInventoryCheck whInventoryCheck)
    {
        startPage();
        WhCheckDetails detail = new WhCheckDetails();
        List<WhInventoryCheck> list = whInventoryCheckService.selectWhInventoryCheckList(whInventoryCheck);
        for (WhInventoryCheck wc : list){
            detail.setCheckCode(wc.getCheckCode());
            List<WhCheckDetails> details = whCheckDetailsService.selectWhCheckDetailsList(detail);
            for (WhCheckDetails whCheckDetails : details) {
                Product product = productService.selectProductName(whCheckDetails.getProductCode());
                whCheckDetails.setProduct(product);
            }
            wc.setWhCheckDetails(details);
        }
        return getDataTable(list);
    }

    /**
     * 导出盘点列表列表
     */
    @RequiresPermissions("warehouse:warehouse:export")
    @Log(title = "盘点列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WhInventoryCheck whInventoryCheck)
    {
        List<WhInventoryCheck> list = whInventoryCheckService.selectWhInventoryCheckList(whInventoryCheck);
        ExcelUtil<WhInventoryCheck> util = new ExcelUtil<WhInventoryCheck>(WhInventoryCheck.class);
        util.exportExcel(response, list, "盘点列表数据");
    }

    /**
     * 获取盘点列表详细信息
     */
    @RequiresPermissions("warehouse:warehouse:query")
    @GetMapping(value = "/{checkId}")
    public AjaxResult getInfo(@PathVariable("checkId") Long checkId)
    {
        return success(whInventoryCheckService.selectWhInventoryCheckByCheckId(checkId));
    }

    /**
     * 新增盘点列表
     */
    @Transactional(rollbackFor = Exception.class)
    @RequiresPermissions("warehouse:warehouse:add")
    @Log(title = "盘点列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WhInventoryCheck whInventoryCheck)
    {
        List<Object> fileList = whInventoryCheck.getFileList();
        pyCodeApi.saveCode(whInventoryCheck.getCodeId(), whInventoryCheck.getNumber());
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("盘点单");
                            pyAttachment.setOwnershipNumber(whInventoryCheck.getCheckCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }

        return toAjax(whInventoryCheckService.insertWhInventoryCheck(whInventoryCheck));
    }

    /**
     * 修改盘点列表
     */
    @RequiresPermissions("warehouse:warehouse:edit")
    @Log(title = "盘点列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WhInventoryCheck whInventoryCheck)
    {
        //删除盘点单
        pyAttachmentService.deletePyAttachmentByOwnershipNumber(whInventoryCheck.getCheckCode());

        //添加盘点单
        List<Object> fileList = whInventoryCheck.getFileList();
        if(fileList.size()>0){
            for (Object file : fileList) {
                if (file instanceof Map) {
                    Map<String, Object> fileMap = (Map<String, Object>) file;
                    Map<String, Object> response = (Map<String, Object>) fileMap.get("response");
                    Object ob=fileMap.get("size");
                    if (response != null) {
                        Map<String, Object> data = (Map<String, Object>) response.get("data");
                        if (data != null) {
                            String name = (String) data.get("name");
                            String url = (String) data.get("url");
                            System.out.println("Name: " + name);
                            System.out.println("URL: " + url);
                            PyAttachment pyAttachment=new PyAttachment();
                            pyAttachment.setBusinessType("盘点单");
                            pyAttachment.setOwnershipNumber(whInventoryCheck.getCheckCode());
                            pyAttachment.setFileName(name);
                            pyAttachment.setSize(Long.parseLong(ob.toString()));
                            pyAttachment.setFilePath(url);
                            pyAttachmentService.insertPyAttachment(pyAttachment);
                        }
                    }
                }
            }
        }

        return toAjax(whInventoryCheckService.updateWhInventoryCheck(whInventoryCheck));
    }

    /**
     * 删除盘点列表
     */
    @RequiresPermissions("warehouse:warehouse:remove")
    @Log(title = "盘点列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{checkIds}")
    public AjaxResult remove(@PathVariable Long[] checkIds)
    {
        return toAjax(whInventoryCheckService.deleteWhInventoryCheckByCheckIds(checkIds));
    }

    /**
     * 修改审核状态
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody WhInventoryCheck whInventoryCheck)
    {
        return toAjax(whInventoryCheckService.updateStatus(whInventoryCheck));
    }
}
